# -*- coding:utf-8 -*-

import golsee
from .. import models
from ..helpers import blog_render
from ..extensions import cache, db
import flask
from flask import g
from . import forms
import datetime


def index(categoryname='all', month='all', page=1):
    category = models.Category.query.filter_by(is_use=1).order_by(models.Category.seq)
    article = models.Article.article_per_page(categoryname, month, page)
    return blog_render("index.html",
                       title='Home',
                       article=article,
                       category=category,
                       categoryname=categoryname,
                       month=month)


def msg():
    form = forms.CommentForm()
    if form.validate_on_submit() and flask.request.method == 'POST':
        comment = models.Comments(name=form.name.data,
                                  comment=form.comment.data)
        if g.user.is_authenticated():
            comment.author = g.user
        comment.timestamp = datetime.datetime.now()
        db.session.add(comment)
        db.session.commit()
        models.Blog_info.new_comment()
        flask.flash(u'评论成功')
        return flask.redirect(flask.url_for('.msg'))
    comments = models.Comments.get_comment(article_id=0)
    return blog_render('main/msg.html', form=form, comments=comments)


# @cache.cached(unless=True)
def about():
    return blog_render('main/about.html')


@cache.cached(unless=True)
def calendar():
    return blog_render('main/calendor.html')
